package com.yao.ro.controller;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.ro.entity.dto.GoodsDto;
import com.yao.ro.entity.qo.GoodsQo;
import com.yao.ro.entity.vo.GoodsVo;
import com.yao.ro.service.GoodsService;

import cn.hutool.core.lang.Console;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 物品表 Controller
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-04-09
 */
@Api(tags = "物品管理")
@RestController
@RequestMapping(value = "/goods")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "新增物品", notes = "新增物品")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@RequestBody GoodsDto model) {

//        Console.log("========================== "+model.getLocalDateTime());


        ValidateHelper.validateNull(model, new String[] {"name", "type", "price"});

        goodsService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改物品", notes = "修改物品")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(@RequestBody GoodsDto model) {

        ValidateHelper.validateNull(model, new String[] {"id", "name", "type", "price"});

        goodsService.modifyById(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "详情", notes = "详情")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ApiResponse<GoodsDto> get(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        return ApiResponse.success(goodsService.getById(id));
    }

    @ApiOperation(value = "删除物品", notes = "逻辑删除物品")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteById(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        goodsService.removeById(id);

        return ApiResponse.success();
    }

    @ApiOperation(value = "商品列表(分页)", notes = "商品列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<IPage<GoodsVo>> list(@RequestBody PageQueryBean<GoodsQo> queryBean) {

        if (Objects.isNull(queryBean.getData())) {
            queryBean.setData(new GoodsQo());
        }

        IPage<GoodsVo> page = goodsService.page(new Page<>(queryBean.getCurrent(), queryBean.getSize()), queryBean.getData());

        return ApiResponse.success(page);
    }

    @ApiOperation(value = "根据商品名称查询商品列表", notes = "根据商品名称查询商品列表")
    @RequestMapping(value = "/listByName", method = RequestMethod.GET)
    public ApiResponse<List<GoodsDto>> listByName(@RequestParam("name") String name) {

        return ApiResponse.success(goodsService.listByName(name));
    }

    @ApiOperation(value = "根据条件查询商品列表", notes = "根据条件查询商品列表")
    @RequestMapping(value = "/listByCondition", method = RequestMethod.POST)
    public ApiResponse<List<GoodsDto>> listByCondition(@RequestBody GoodsDto condition) {

        return ApiResponse.success(goodsService.listByCondition(condition));
    }

}